package com.pengbei.enshrine.common.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;

import androidx.annotation.NonNull;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.security.MessageDigest;
import java.util.Objects;

public class FilletTransformation extends BitmapTransformation {
    private float radius;

    private boolean isLeftTop, isRightTop, isLeftBottom, isRightBotoom;

    public FilletTransformation(int radius, boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom){
        isLeftTop = leftTop;
        isRightTop = rightTop;
        isLeftBottom = leftBottom;
        isRightBotoom = rightBottom;
        this.radius = radius;
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        Bitmap resultBitmap = pool.get(outWidth,outHeight, getAlphaSafeConfig(toTransform));
        BitmapShader bitmapShader = new BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        float scaleMax = 1.0f;
        // 如果图片的宽或者高与view的宽高不匹配，计算出需要缩放的比例；缩放后的图片的宽高，一定要大于我们view的宽高；所以我们这里取大值；
        if (outWidth != toTransform.getWidth() || outHeight !=toTransform.getHeight()){
            scaleMax = Math.max((float) outWidth / (float)toTransform.getWidth(),(float)outHeight / (float)toTransform.getHeight());
        }
        Matrix matrix = new Matrix();
        matrix.setScale(scaleMax,scaleMax);
        bitmapShader.setLocalMatrix(matrix);
        Paint mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setShader(bitmapShader);
        Canvas canvas = new Canvas(resultBitmap);
        RectF drawRectF = new RectF(0,0,outWidth,outHeight);
        canvas.drawRoundRect(drawRectF,radius,radius,mPaint);
        //左上角圆角
        if (!isLeftTop) {
            canvas.drawRect(0, 0, radius, radius, mPaint);
        }
        //右上角圆角
        if (!isRightTop) {
            canvas.drawRect(canvas.getWidth() - radius, 0, canvas.getWidth(), radius, mPaint);
        }
        //左下角圆角
        if (!isLeftBottom) {
            canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), mPaint);
        }
        //右下角圆角
        if (!isRightBotoom) {
            canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), mPaint);
        }
        clear(canvas);
        return resultBitmap;
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FilletTransformation that = (FilletTransformation) o;
        return Float.compare(that.radius, radius) == 0 &&
                isLeftTop == that.isLeftTop &&
                isRightTop == that.isRightTop &&
                isLeftBottom == that.isLeftBottom &&
                isRightBotoom == that.isRightBotoom;
    }

    @Override
    public int hashCode() {
        return Objects.hash(radius, isLeftTop, isRightTop, isLeftBottom, isRightBotoom);
    }

    // Avoids warnings in M+.
    private void clear(Canvas canvas) {
        canvas.setBitmap(null);
    }

    private Bitmap.Config getAlphaSafeConfig(@NonNull Bitmap inBitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Avoid short circuiting the sdk check.
            if (Bitmap.Config.RGBA_F16.equals(inBitmap.getConfig())) { // NOPMD
                return Bitmap.Config.RGBA_F16;
            }
        }
        return Bitmap.Config.ARGB_8888;
    }
}